<template>
  <!-- 项目明细 -->
  <div class="project-detail">
    <el-dialog
      :title="titleObj[type]"
      :visible.sync="dialogVisible"
      width="100%"
      custom-class="add-parameter-dialog"
      :before-close="handleClose"
      :append-to-body="true"
    >
      <div class="project-detail-content" v-loading="loading">
        <el-form
          :model="formData"
          label-width="100px"
          :rules="type == 3 ? { required: false } : rules"
          ref="projectForm"
          :disabled="type == 3"
          size="small"
        >
          <div>
            <el-row>
              <el-form-item
                :label="$t('项目编号')"
                prop="projectNumber"
                :rules="rules.projectNumber"
              >
                <el-input
                  :disabled="type != 1"
                  style="width: 100%"
                  v-model="formData.projectNumber"
                  size="small"
                ></el-input>
              </el-form-item>
            </el-row>
          </div>
          <template v-if="visible">
            <div
              class="item-info"
              v-for="(item, index) in formList"
              :key="index"
            >
            <div class="title">{{ item.parameterClassifyName }}</div>
              <div>
                <el-row v-for="(it, idx) in item.parameterDtoList" :key="idx">
                  <el-form-item
                    v-if="it.inputFlag !== 2"
                    :label="it.parameterName"
                    :prop="it.parameterNumber"
                  >
                    <!-- 1 文本,2 数字,3 单选,4 复选,5 日期和时间,6 文本域,7 附件,8 主数据,9 非标准 -->
                    <template v-if="it.parameterType == 1">
                      <el-input
                        style="width: 27%"
                        v-model="formData[it.parameterNumber]"
                        :maxlength="it.parameterValue.textLimit"
                        show-word-limit
                        :disabled="it.state === 2"
                        :placeholder="it.promptTemplate"
                        @blur="changeRule(it)"
                      ></el-input>
                    </template>
                    <template v-if="it.parameterType == 2">
                      <el-input
                        style="width: 27%"
                        v-model="formData[it.parameterNumber]"
                        maxlength="21"
                        :disabled="it.state === 2"
                        :placeholder="it.promptTemplate"
                        @blur="changeRule(it)"
                      ></el-input>
                    </template>
                    <template
                      v-if="it.parameterType == 3 || it.parameterType == 4"
                    >
                      <!-- 关联数据 -->
                      <template v-if="it.parameterValue.enumType == 2">
                        <dataSource
                          v-model="formData[it.parameterNumber]"
                          :ref="it.parameterNumber"
                          :disabled="it.state === 2"
                          :parameter="it"
                          :detail="detail"
                          :current-form="formData"
                          @setrules="setRules"
                          @changeRule="changeRule(it)"
                        ></dataSource>
                      </template>
                      <template v-else>
                        <el-select
                          v-model="formData[it.parameterNumber]"
                          :disabled="it.state === 2"
                          :multiple="it.parameterType === 4"
                          :placeholder="it.promptTemplate"
                          @change="changeRule(it)"
                        >
                          <el-option
                            v-for="option in it.parameterValue.optionsStr"
                            :key="option.optionName"
                            :label="option.optionDesc || option.optionName"
                            :value="option.optionName"
                          ></el-option>
                        </el-select>
                      </template>
                    </template>
                    <!-- 时间 -->
                    <template v-if="it.parameterType === 5">
                      <el-date-picker
                        v-model="formData[it.parameterNumber]"
                        :disabled="it.state === 2"
                        :type="it.parameterValue.dateType"
                        :format="it.parameterValue.format"
                        :value-format="it.parameterValue.valueFormat"
                        :range-separator="it.parameterValue.rangeSeparator"
                        :start-placeholder="it.parameterValue.startPlaceholder"
                        :end-placeholder="it.parameterValue.endPlaceholder"
                        :placeholder="it.promptTemplate"
                        @change="changeRule(it)"
                      >
                      </el-date-picker>
                    </template>
                    <!-- 文本域 -->
                    <template v-if="it.parameterType === 6">
                      <el-input
                        v-model="formData[it.parameterNumber]"
                        type="textarea"
                        :maxlength="it.parameterValue.textLimit"
                        show-word-limit
                        :disabled="it.state === 2"
                        :rows="6"
                        :placeholder="it.promptTemplate"
                        @blur="changeRule(it)"
                      ></el-input>
                    </template>
                    <!-- 附件 -->
                    <template v-if="it.parameterType === 7">
                      <file
                        :ref="it.parameterNumber"
                        v-if="it.parameterNumber"
                        v-model="formData[it.parameterNumber]"
                        :parameter="it"
                        :disabled="it.state === 2"
                        :detail="detail"
                        :pageType="pageType"
                        :definitionId="id"
                        :current-form="formData"
                      ></file>
                    </template>
                    <template v-if="it.parameterType === 9">
                      <component
                        :ref="it.parameterNumber"
                        :is="it.parameterNumber"
                        v-if="it.parameterNumber"
                        v-model="formData[it.parameterNumber]"
                        :parameter="it"
                        :data="formData"
                        :disabled="it.state === 2"
                        :detail="detail"
                        :type="type"
                        :warehouseId="warehouseId"
                        :warehouse="warehouse"
                        :tableData="tableData"
                        @setrules="setRules"
                        @feeChange="feeChange"
                        @billingMethodsChange="billingMethodsChange"
                      ></component>
                    </template>
                    <!-- 选人 -->
                    <template v-if="it.parameterType === 10">
                      <userComponent
                        v-model="formData[it.parameterNumber]"
                        :ref="it.parameterNumber"
                        :disabled="it.state === 2"
                        :parameter="it"
                        :detail="detail"
                        :current-form="formData"
                        @setrules="setRules"
                        @changeRule="changeRule(it)"
                      ></userComponent>
                    </template>
                    <!-- 地区 -->
                    <template v-if="it.parameterType === 11">
                      <areaSelected
                        v-model="formData[it.parameterNumber]"
                        :ref="it.parameterNumber"
                        :disabled="it.state === 2"
                        :parameter="it"
                        :detail="detail"
                        :current-form="formData"
                        @setrules="setRules"
                        @changeRule="changeRule(it)"
                      ></areaSelected>
                    </template>
                    <!-- 选电子文本 -->
                    <template v-if="it.parameterType === 12">
                      <electronicText
                        v-model="formData[it.parameterNumber]"
                        :ref="it.parameterNumber"
                        :disabled="it.state === 2"
                        :parameter="it"
                        :detail="detail"
                        :current-form="formData"
                        @setrules="setRules"
                        @changeRule="changeRule(it)"
                      ></electronicText>
                    </template>
                    <!-- 号码类 -->
                    <template v-if="it.parameterType === 13">
                      <el-input
                        v-model="formData[it.parameterNumber]"
                        :disabled="it.state === 2"
                        :placeholder="it.promptTemplate"
                        @blur="changeRule(it)"
                      ></el-input>
                    </template>
                  </el-form-item>
                </el-row>
              </div>
            </div>
          </template>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose" :disabled="false">{{
          $t("关闭")
        }}</el-button>
        <el-button type="primary" @click="confirm" v-if="type != 3">{{
          $t("确认 ")
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import Vue from 'vue'
import contractCreationService from "@/services/contractCreationService";
import eventBus from "@/assets/utils/eventBus.js";
import api from "@/services/defineContract";
import projectNonStandard from "./projectNonStandard";
import parkProjectNonStandard from "./parkProjectNonStandard";
import file from "./nonStandard/files";
// 选人组件
import userComponent from "@/views/personalCenter/components/controlType/userComponent.vue";
// 关联数据
import dataSource from "@/views/personalCenter/components/controlType/dataSource.vue";
// 选电子文本
import electronicText from "@/views/personalCenter/components/controlType/electronicText.vue";
//动态加载非标准组件
projectNonStandard.forEach((it) => {
  Vue.component(it.name, it);
});
parkProjectNonStandard.forEach((it) => {
  Vue.component(it.name, it);
});
export default {
  components: { file, userComponent, dataSource, electronicText },
  props: {
    dialogVisible: Boolean,
    type: Number,
    id: {},
    code: String,
    rowList: {
      type: Object,
      default: () => {},
    },
    pageType: {
      type: String,
      default: "",
    },
    detail: {
      type: Object,
      default: () => {
        return {};
      },
    },
    tableData: {
      type: Array,
      default: () => [],
    },
    index: {
      type: Number,
      default: null,
    },
    projectDetailCode: String,
    headerValueList: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    const numberValid = (rule, value, callback) => {
      if (this.type == 3) {
        callback();
      } else if (value && !/^\d{1,4}$/.test(value)) {
        callback(new Error(this.$t("项目编号须为数字")));
      } else if (
        this.tableData.find(
          (it, idx) => it.projectNumber == value && idx != this.index
        )
      ) {
        callback(
          new Error(this.$t("该项目编号已经在项目明细里面存在，请重新输入"))
        );
      } else {
        callback();
      }
    };
    return {
      formData: {},
      rules: {
        projectNumber: [
          { required: false, validator: numberValid, trigger: "blur" },
        ],
      },
      titleObj: {
        1: this.$t("添加项目明细"),
        2: this.$t("修改项目明细"),
        3: this.$t("项目明细详情"),
      },
      productList: [],
      billingList: [],
      costList: [],
      readonly: false,
      formList: [],
      originList: [],
      visible: false,
      userType: null,
      intervalNumber: null,
      warehouseId: null,
      warehouse: {},
      loading: false,
    };
  },
  created() {
    if (this.type == 1) {
      this.projectAdd();
    } else {
      let costCode = null;
      let billingCode = null;
      this.rowList.parameterDtoList &&
        this.rowList.parameterDtoList.map((item) => {
          if (item.parameterNumber == "fee") {
            costCode =
              item.projectValue && JSON.parse(item.projectValue).checkedData;
          }
          if (item.parameterNumber == "billingMethods") {
            billingCode =
              item.projectValue && JSON.parse(item.projectValue).checkedData;
          }
        });
      let params = {
        costCode,
        billingCode,
      };
      this.projectAdd(params, true);
    }
  },
  methods: {
    feeChange(val) {
      let params = {
        costCode: val,
      };
      this.projectAdd(params);
    },
    billingMethodsChange(params) {
      this.projectAdd(params);
    },
    // 获取页面已填写的抬头信息
    getHeaderInfo() {
      let arr = (this.headerValueList || []).map((it) => {
        return {
          ruleFieldKey: it.ruleFieldKey,
          ruleValue: it.headerValue,
        };
      });
      return arr;
    },
    // 获取新增数据
    projectAdd(data, flag) {
      let type = 1;
      if (this.rowList.detailId) {
        type = 2;
        if (this.type == 3) {
          type = 4;
        }
      } else {
        type = 1;
      }
      let params = {
        definitionCode: this.code
          ? this.code
          : this.$route.query.definitionCode,
        type: type,
        costCode: data && data.costCode,
        billingCode: (data && data.billingCode) || "1001",
        projectDetailCode: this.projectDetailCode,
        filledRuleFieldList: this.getHeaderInfo(),
      };
      if (this.rowList.changeFlag) {
        params = {
          ...params,
          ...this.rowList,
        };
      }
      if (this.type != 1) {
        this.formData.projectNumber = this.rowList.projectNumber;
      }
      if (this.pageType == "template") {
        let vm = this;
        api.projectAdd(params).then((res) => {
          if (res.code == 200) {
            this.intervalNumber = res.intervalNumber;
            this.warehouseId = res.warehouse
              ? JSON.parse(res.warehouse).value
              : "";
            this.warehouse = res.warehouse ? JSON.parse(res.warehouse) : {};
            if (res.park) {
              const park = res.park ? JSON.parse(res.park) : {};
              localStorage.setItem("parkCode", park.checkedData);
            }
            if (res.startEndTime) {
              const time = res.startEndTime
                ? JSON.parse(res.startEndTime).value
                : "";
              eventBus.$emit("startEndTimeChange", time);
            }
            if (this.type != 1) {
              /**将列表数据赋给默认值 */
              this.formList = (res.projectClassifyList || []).map((item) => {
                (item.parameterProjectList || []).map((it) => {
                  (this.rowList.parameterDtoList || []).map((its) => {
                    if (it.parameterNumber == its.parameterNumber) {
                      it.defaultValue = its.defaultValue
                        ? its.defaultValue
                        : it.defaultValue;
                      it.state = item.state ? item.state : it.state;
                    }
                  });
                  return it;
                });
                return item;
              });
            } else {
              this.formList = (res.projectClassifyList || []).map((it) => {
                return {
                  ...it,
                  parameterDtoList: it.parameterProjectList,
                };
              });
            }
            this.$store.dispatch("contract/setProjectForm", {});
            this.setData();
          }
        });
      } else {
        this.loading = true;
        api
          .contractFormProjectAdd({
            ...params,
            // type: this.type === 3 ? 4: undefined//contractFormProjectAdd  type 4 详情
          })
          .then((res) => {
            if (res.code == 200) {
              this.loading = false;
              this.intervalNumber = res.data.intervalNumber;
              let data = res.data.parameterProduct || [];
              if (this.type != 1) {
                /**将列表数据赋给默认值 */
                this.formList = (data || []).map((item) => {
                  (item.parameterProjectList || []).map((it) => {
                    (this.rowList.parameterProductList || []).map((its) => {
                      if (it.parameterNumber == its.parameterNumber) {
                        it.defaultValue = its.defaultValue;
                      }
                    });
                    return it;
                  });
                  return item;
                });
              } else {
                this.formList = data;
              }
              this.$store.dispatch("contract/setProjectForm", {});
              this.setData();
            }
          })
          .catch(() => (this.loading = false));
      }
    },
    handleClose() {
      this.$emit("closeProjectDetail");
      localStorage.removeItem("goodsType");
      localStorage.removeItem("propertyType");
      localStorage.removeItem("modelType");
      localStorage.removeItem("categoryType");
    },
    setData() {
      let vm = this;
      let data = JSON.parse(JSON.stringify(this.formList)) || [];
      data = data.map((it) => {
        return {
          ...it,
          parameterDtoList: it.parameterProjectList || it.parameterDtoList,
        };
      });
      let list = data.map((item) => {
        item.parameterDtoList = (item.parameterDtoList || []).map((it) => {
          let obj = {
            ...it,
            parameterValue: it.parameterValueStr
              ? JSON.parse(it.parameterValueStr)
              : {},
          };
          //  timeCount 1-单项 2-双项 timeType:1-日期 2-时间:
          if (obj.parameterType === 5) {
            let dateType = "date";
            let format = "yyyy-MM-dd";
            let valueFormat = "yyyy-MM-dd";
            let rangeSeparator = null;
            let startPlaceholder = null;
            let endPlaceholder = null;
            if (
              obj.parameterValue.timeCount === 1 &&
              obj.parameterValue.timeType === 1
            ) {
              dateType = "date";
            }
            if (
              obj.parameterValue.timeCount === 1 &&
              obj.parameterValue.timeType === 2
            ) {
              dateType = "datetime";
              format = "yyyy-MM-dd HH:mm:ss";
              valueFormat = "yyyy-MM-dd HH:mm:ss";
            }
            if (
              obj.parameterValue.timeCount === 2 &&
              obj.parameterValue.timeType === 1
            ) {
              dateType = "daterange";
              format = "yyyy-MM-dd";
              valueFormat = "yyyy-MM-dd";
              rangeSeparator = this.$t("至");
              startPlaceholder = this.$t("开始日期");
              endPlaceholder = this.$t("截止日期");
            }
            if (
              obj.parameterValue.timeCount === 2 &&
              obj.parameterValue.timeType === 2
            ) {
              dateType = "datetimerange";
              format = "yyyy-MM-dd HH:mm:ss";
              valueFormat = "yyyy-MM-dd HH:mm:ss";
              rangeSeparator = this.$t("至");
              startPlaceholder = this.$t("开始时间");
              endPlaceholder = this.$t("截止时间");
            }
            obj.parameterValue = {
              ...obj.parameterValue,
              optionsStr: obj.parameterValue.optionsStr || [],
              dateType: dateType,
              format: format,
              valueFormat: valueFormat,
              rangeSeparator: rangeSeparator,
              startPlaceholder: startPlaceholder,
              endPlaceholder: endPlaceholder,
            };
          }
          let defaultValue = it.defaultValue;
          if (!defaultValue) {
            let parameterNumber =
              vm.rowList.headerValue &&
              vm.rowList.headerValue.find(
                (item) => item.parameterNumber === it.parameterNumber
              );
            if (parameterNumber) {
              defaultValue = parameterNumber.headerValue || null;
            } else {
              defaultValue = null;
            }
          }
          if (defaultValue) {
            vm.$set(vm.formData, it.parameterNumber, defaultValue);
          }
          if (
            obj.parameterValue.timeCount == 2 &&
            this.formData[it.parameterNumber]
          ) {
            if (it.defaultValue) {
              if (typeof it.defaultValue == "string") {
                vm.$set(
                  vm.formData,
                  it.parameterNumber,
                  it.defaultValue.split(",")
                );
              } else {
                vm.$set(vm.formData, it.parameterNumber, it.defaultValue);
              }
            }
          }
          vm.setRules(obj);
          return obj;
        });
        return {
          ...item,
        };
      });
      this.originList = list; //将处理后的数组赋给originList
      this.formList = list;
      this.visible = true;
      // 数据更改，对应选择不限的数据重新赋值
      if (this.visible) {
        eventBus.$emit("changeValue");
      }
    },
    setRules(parameter, rules) {
      let vm = this;
      if (rules) {
        this.rules = {
          ...this.rules,
          ...rules,
        };
      } else if (parameter.parameterType === 2) {
        const rules = [
          {
            required: parameter.inputFlag == 1,
            validator: (rule, value, callback) => {
              let reg = null;
              if (parameter.parameterValue.decimalLimit) {
                // '^-?[0-9]{1,' + parameter.parameterValue.integerLimit+'}(.[0-9]{1,'+parameter.parameterValue.decimalLimit+'})?$
                reg = new RegExp(
                  `^(-)*([0-9]{1,${parameter.parameterValue.integerLimit}})(\\.[0-9]{0,${parameter.parameterValue.decimalLimit}})?$`
                );
              } else {
                reg = new RegExp(
                  `^(-)*([0-9]{1,${parameter.parameterValue.integerLimit}})(\\.[0-9]{0,8})?$`
                );
              }
              let maxValue = parameter.parameterValue.maxValue
                ? parameter.parameterValue.maxValue
                : null;
              let minValue = parameter.parameterValue.minValue
                ? parameter.parameterValue.minValue
                : null;
              let decimalLimit = parameter.parameterValue.decimalLimit
                ? parameter.parameterValue.decimalLimit
                : 8;
              if (parameter.inputFlag == 1) {
                if (!value) {
                  callback(
                    new Error(parameter.parameterName + this.$t("是必填项"))
                  );
                } else if (!reg.test(value)) {
                  callback(
                    new Error(
                      this.$t("只能输入整数最多") +
                        parameter.parameterValue.integerLimit +
                        this.$t("位,小数") +
                        decimalLimit +
                        this.$t("位")
                    )
                  );
                } else if (
                  (maxValue && Number(value) > maxValue) ||
                  (minValue && Number(value) < minValue)
                ) {
                  if (maxValue && Number(value) > maxValue) {
                    callback(
                      new Error(
                        this.$t("请输入比") + maxValue + this.$t("小的值")
                      )
                    );
                  }
                  if (minValue && Number(value) < minValue) {
                    callback(
                      new Error(
                        this.$t("请输入比") + minValue + this.$t("大的值")
                      )
                    );
                  }
                } else {
                  callback();
                }
              } else {
                if (value && !reg.test(value)) {
                  callback(
                    new Error(
                      this.$t("只能输入整数最多") +
                        parameter.parameterValue.integerLimit +
                        this.$t("位,小数") +
                        decimalLimit +
                        this.$t("位")
                    )
                  );
                } else if (
                  (maxValue && Number(value) > maxValue) ||
                  (minValue && Number(value) < minValue)
                ) {
                  if (maxValue && Number(value) > maxValue) {
                    callback(
                      new Error(
                        this.$t("请输入比") + maxValue + this.$t("小的值")
                      )
                    );
                  }
                  if (minValue && Number(value) < minValue) {
                    callback(
                      new Error(
                        this.$t("请输入比") + minValue + this.$t("大的值")
                      )
                    );
                  }
                } else {
                  callback();
                }
              }
            },
            trigger: ["blur", "change"],
          },
        ];
        this.$set(this.rules, parameter.parameterNumber, rules);
      } else if (parameter.parameterType === 13) {
        const rules = [
          {
            required: parameter.inputFlag == 1,
            validator: (rule, value, callback) => {
              let reg = null;
              let errMsg = "";
              // 手机号
              if (parameter.parameterValue.numberType == 1) {
                reg = /^1\d{10}$/;
                errMsg = this.$t("请输入正确的手机号");
              } else if (parameter.parameterValue.numberType == 2) {
                // 身份证号
                reg =
                  /^[1-9]\d{5}(?:18|19|20)\d{2}(?:0[1-9]|10|11|12)(?:0[1-9]|[1-2]\d|30|31)\d{3}[\dXx]$/;
                errMsg = this.$t("请输入正确的身份证号");
              }
              if (parameter.inputFlag == 1) {
                if (!value) {
                  callback(
                    new Error(parameter.parameterName + this.$t("是必填项"))
                  );
                } else if (!reg.test(value)) {
                  callback(new Error(errMsg));
                } else {
                  callback();
                }
              } else {
                if (value && !reg.test(value)) {
                  callback(new Error(errMsg));
                } else {
                  callback();
                }
              }
            },
            trigger: ["blur", "change"],
          },
        ];
        this.$set(this.rules, parameter.parameterNumber, rules);
      } else {
        let type = "string";
        if (
          parameter.parameterType === 5 &&
          parameter.parameterValue.timeCount == 2 || parameter.parameterType === 4
        ) {
          type = "array";
        }
        if (parameter.parameterType === 8) {
          type = "object";
        }
        this.rules = {
          ...this.rules,
        };
        const rules = [
          {
            required: parameter.inputFlag === 1,
            message: parameter.parameterName + this.$t("是必填项"),
            trigger: ["blur", "change"],
            type: type,
          },
        ];
        this.$set(this.rules, parameter.parameterNumber, rules);
      }
    },
    changeRule(row) {
      if (!(row.relatedRuleFieldList && row.relatedRuleFieldList.length)) {
        return;
      }
      let filledRuleFieldList = this.getArrData();
      filledRuleFieldList = (filledRuleFieldList || []).map((it) => {
        return {
          ruleFieldKey: it.ruleFieldKey,
          ruleValue: it.projectValue,
        };
      });
      const params = {
        definitionId: this.id,
        relatedRuleFieldList: row.relatedRuleFieldList,
        filledRuleFieldList,
      };
      this.loading = true;
      contractCreationService
        .calcTemplateDefinitionRuleField(params)
        .then((res) => {
          this.loading = false;
          // 项目明细规则处理
          const projectArr = res.data.projectRuleFieldCalculateDtoList;
          // 根据规则塞值
          this.formList = (this.formList || []).map((its) => {
            (its.parameterDtoList || []).map((it) => {
              (projectArr || []).map((item) => {
                if (it.parameterNumber === item.parameterNumber) {
                  let defaultValue = item.defaultValue;
                  const parameterValue = JSON.parse(item.parameterValueStr) || {};
                  // 处理数组格式
                  if ((parameterValue.timeCount == 2 || item.parameterType == 4) && defaultValue) {
                    if (JSON.parse(defaultValue).value) {
                      defaultValue = JSON.parse(defaultValue).value;
                    } else {
                      defaultValue = JSON.parse(defaultValue);
                    }
                  }
                  it.defaultValue = defaultValue ? defaultValue : it.defaultValue;
                  it.state = item.state;
                  it.inputFlag = item.inputFlag;
                }
              });
              return it;
            });
            return its;
          });
          this.setData();
        })
        .catch(() => (this.loading = false));
    },
    getArrData() {
      let vm = this;
      let data = [];
      vm.originList.map((it) => {
        data = data.concat(it.parameterDtoList);
      });
      let parameterList = data.map((it) => {
        let projectValue =
          typeof vm.formData[it.parameterNumber] == "string"
            ? vm.formData[it.parameterNumber]
            : JSON.stringify(vm.formData[it.parameterNumber]);
        // 双项时间选择器,将数组转成字符串
        if (
          it.parameterValue.timeCount == 2 &&
          this.formData[it.parameterNumber] instanceof Array
        ) {
          let obj = { value: this.formData[it.parameterNumber] };
          projectValue = JSON.stringify(obj);
        }
        //对附件 主数据做特殊处理
        if ([7, 8, 9].includes(it.parameterType)) {
          let obj = vm.$refs[it.parameterNumber][0].getData();
          projectValue = obj ? JSON.stringify(obj) : null;
        }
        //非标
        if ([9].includes(it.parameterType) && it.inputFlag != 2) {
          let obj = vm.$refs[it.parameterNumber][0].getData();
          projectValue = JSON.stringify(obj);
        }
        return {
          ...it,
          projectValue,
        };
      });
      return parameterList;
    },
    getData() {
      let vm = this;
      vm.$refs.projectForm.validate().then(() => {
        let parameterList = (this.getArrData() || []).map((it) => {
          return {
            parameterId: it.parameterId,
            parameterName: it.parameterName,
            parameterNumber: it.parameterNumber,
            projectValue: it.projectValue,
            parameterType: it.parameterType,
          };
        });
        // 项目编号
        let projectNumber = null;
        if (!this.formData.projectNumber) {
          // 生成编号需要是间隔的整数倍,且大于已添加的项目编号
          let tableData = JSON.parse(JSON.stringify(this.tableData));
          if (tableData && tableData.length) {
            // 取出最大值
            let data = tableData && tableData.map((it) => it.projectNumber);
            let num = Math.max(...data);
            // 计算整数倍
            let diff = Math.floor(num / this.intervalNumber);
            projectNumber = this.intervalNumber * diff + this.intervalNumber;
          } else {
            projectNumber = this.intervalNumber;
          }
        }
        let projectDetailList = {
          projectNumber: this.formData.projectNumber || projectNumber,
          detailId: this.formData.detailId,
          projectParameterList: parameterList,
        };
        // type 1新增 2修改 3详情
        if (this.type == 1) {
          this.$emit("addData", projectDetailList);
        }
        if (this.type == 2) {
          this.$emit("editData", projectDetailList);
        }
        this.handleClose();
        return projectDetailList;
      });
    },
    confirm() {
      if (this.type != 3) {
        this.getData();
      } else {
        this.handleClose();
      }
    },
  },
};
</script>
<style lang="scss" scoped>
  .project-detail-content {
    .item-info {
      background: #fff;

      margin-bottom: 24px;
      .title {
        font-size: 13px;
        font-weight: 500;
        color: #333333;
        margin-bottom: 10px;
        &::before {
          content: '';
          display: inline-block;
          width: 2px;
          height: 14px;
          background: #364dd7;
          margin-right: 12px;
        }
      }
    }
  }
</style>
